package com.tianji.remark.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tianji.common.autoconfigure.mq.RabbitMqHelper;
import com.tianji.common.domain.dto.LikedTimesDTO;
import com.tianji.common.utils.StringUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.remark.domain.dto.LikeRecordFormDTO;
import com.tianji.remark.domain.po.LikedRecord;
import com.tianji.remark.mapper.LikedRecordMapper;
import com.tianji.remark.service.ILikedRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.tianji.common.constants.MqConstants.Exchange.LIKE_RECORD_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.LIKED_TIMES_KEY_TEMPLATE;

/**
 * <p>
 * 点赞记录表 服务实现类
 * </p>
 *
 * @author huanghui
 * @since 2023-07-27
 */
//@Service
@Slf4j
@RequiredArgsConstructor
public   class LikedRecordServiceImpl extends ServiceImpl<LikedRecordMapper, LikedRecord> implements ILikedRecordService {

    private  final RabbitMqHelper mqHelper;


    @Override
    public void addLikeRecord(LikeRecordFormDTO recordDTO) {
        //1.recordDTO,判断是执行点赞还是取消点赞
        Boolean success = recordDTO.getLiked() ? like(recordDTO):unLike(recordDTO);
        //2.判断是否执行成功，如果失败，则直接结束
        if (!success){
            return;
        }
        //3.如果执行成功，统计点赞数量
        Integer likeTimes = lambdaQuery().eq(LikedRecord::getBizId, recordDTO.getBizId())
                .count();
        //4.发送MQ通知
        mqHelper.send(
                LIKE_RECORD_EXCHANGE,
                StringUtils.format(LIKED_TIMES_KEY_TEMPLATE,recordDTO.getBizType()),
                LikedTimesDTO.of(recordDTO.getBizId(), likeTimes)
        );
    }

    @Override
    public Set<Long> isBizLiked(List<Long> bizIds) {
        //1.查询登录用户id
        Long userId = UserContext.getUser();
        //2.查询点赞状态
        List<LikedRecord> list = lambdaQuery()
                .in(LikedRecord::getBizId, bizIds)
                .eq(LikedRecord::getUserId, userId)
                .list();
        //3.返回结果
        Set<Long> bizidsSet = list.stream().map(LikedRecord::getBizId).collect(Collectors.toSet());
        return bizidsSet;
    }

    @Override
    public void readLikedTimesAndSendMessage(String bizType, int maxBizSize) {

    }

    private Boolean unLike(LikeRecordFormDTO recordDTO) {
        boolean flag = remove(new QueryWrapper<LikedRecord>().lambda().eq(LikedRecord::getBizId, recordDTO.getBizId())
                .eq(LikedRecord::getUserId, UserContext.getUser()));
        return flag;
    }

    private Boolean like(LikeRecordFormDTO recordDTO) {
        //1.查询点赞记录
        Integer count = lambdaQuery()
                .eq(LikedRecord::getUserId, UserContext.getUser())
                .eq(LikedRecord::getBizId, recordDTO.getBizId())
                .count();
        //2.判断是否存在，如果以及存在，直接结束
        if (count>0){
            return false;
        }
        //3.如果不存在，直接新增
        LikedRecord record = new LikedRecord();
        record.setBizId(recordDTO.getBizId());
        record.setBizType(recordDTO.getBizType());
        record.setUserId(UserContext.getUser());
        save(record);
        return true;
    }
}
